import { BrowserWindow, BrowserView, app } from "electron"
import IpcTemplate from "../core/ipc/ipc-template"
import path from "path"
import { distPath } from "@/main/core/project-path"
import { constTemplate } from "@/common/constants"

class MainWindowIpc extends IpcTemplate {
  private mWindow:BrowserWindow
  // #browserView;

  constructor() {
    super(constTemplate.mainWindow)
  }

  createWindow() {
    // Create the browser window.
    this.mWindow = new BrowserWindow({
      width: 1080,
      height: 840,
      webPreferences: {
        preload: app.isPackaged ? 
                  path.join(__dirname,"preload.js") : 
                  path.join(distPath, 'preload.js')
      }
    })
    // this.#browserView = new BrowserView({
    //   webPreferences: {
    //     preload: path.join(webpackPath.distPath, 'preload.js')
    //   }
    // })
    // this.#mainWindow.setBrowserView(this.#browserView)
    // this.#browserView.setBounds({ x: 250, y: 0, width: 830, height: 840 })
    // this.#browserView.setAutoResize({ width: true, height: true })
    // this.#browserView.webContents.openDevTools({mode:'detach'})
    // 加载 index.html
    if (app.isPackaged) {
      this.mWindow.loadFile(path.join(__dirname,"index.html"))
    } else {
      this.mWindow.loadURL("http://localhost:3000/")
    }
    // 打开开发工具
    // this.#mainWindow.webContents.openDevTools({mode:'detach'})
  }
  // /**
  //  * 
  //  * @param { params.filePath } 需要加载html本地路径  
  //  */
  // loadFile(params) {
  //   const file = "./renderer-process/" + params.filePath
  //   this.#browserView.webContents.loadFile(file)
  // }

  // /**
  //  * 打开主窗口开发者工具
  //  */
  // openMainDevTools() {
  //   if (this.#mainWindow) {
  //     this.#mainWindow.webContents.openDevTools({ mode: 'detach' })
  //   }
  // }

  // /**
  //  * 打开webView开发者工具
  //  */
  //   openWebViewDevTools() {
  //     if (this.#browserView) {
  //       this.#browserView.webContents.openDevTools({ mode: 'detach' })
  //     }
  //   }

}

const mainWindow = new MainWindowIpc()

export default mainWindow
